'use strict';

const Controller = require('egg').Controller;

// 其它
class OtherController extends Controller {
  // 查询一个erc20的精度
  async getDecimals() {
    const { app, ctx, service } = this;
    const address = ctx.request.body.address;
    const data = await service.erc20.decimals2(address);
    ctx.body = { code: 200, msg: '请求成功', data: data};
  }

  // 查询合约默认区块高度和当前区块高度
  async getBeginBlock() {
    const { app, ctx, service } = this;
    // 合约部署的区块高度
    const beginBlock = app.Init.beginBlock;
    // 查询这个区块高度的详情
    const beginBlockMsg = app.Init.timestamp;// await service.eth.getBlockMsg(beginBlock);
    // 获取当前的区块高度
    const nowBlock = await service.eth.getBlockNumber2();
    const nowBlockMsg = await service.eth.getBlockMsg(nowBlock);
    const data = {
      beginBlock: beginBlock,
      beginBlockTime: beginBlockMsg,
      nowBlock: nowBlock,
      nowBlockTime: nowBlockMsg.block_header.raw_data.timestamp,
    };
    ctx.body = { code: 1000, msg: '发送成功', data: data};
  }

  // 波场默认地址格式转换成0x开头的地址格式
  async trxTo0x() {
    const { app, ctx, service } = this;
    // 合约部署的区块高度
    const address = ctx.request.body.address;
    const res = await service.eth.addressToHex2(address);
    ctx.body = { code: 1000, msg: '发送成功', data: res};
  }

  // 获取token的价格
  async getTokenPrice() {
    const { app, ctx, service } = this;
    // 合约部署的区块高度
    const tokenAddress = ctx.request.body.tokenAddress;
    if(service.eth.isAddress(tokenAddress) === false) {
      ctx.body = { code: 1001, msg: '这个地址不合法', data: '1' };
      return;
    }

    const res = await service.price.getTokenPrice2(tokenAddress);
    ctx.body = { code: 1000, msg: '发送成功', data: res};
  }

  // 获取lp的价格
  async getLpPrice() {
    const { app, ctx, service } = this;
    // 合约部署的区块高度
    const lpAddress = ctx.request.body.lpAddress;
    if(service.eth.isAddress(lpAddress) === false) {
      ctx.body = { code: 1001, msg: '这个地址不合法', data: '1' };
      return;
    }

    const res = await service.price.getLpPrice2(lpAddress);
    ctx.body = { code: 1000, msg: '发送成功', data: res};
  }

  // 查询二级管理员地址
  getOwnerAddress() {
    const { app, ctx, service } = this;
    const address = app.BSC.OwnerAddress;
    ctx.body = { code: 1000, msg: '发送成功', data: address};
  }

  // 查询地址的gdcv余额
  async getGdcvBalance() {
    const { app, ctx, service } = this;
    // 合约部署的区块高度
    const address = ctx.request.body.address;
    const gdcvContract = app.BSC.HnftContract;

    let res = await gdcvContract.methods.balanceOf(address).call();
    if(res._hex === undefined) {
      res = res.balance;
      res = res._hex;
      res = ctx.helper.hexToDec(res);
      ctx.body = { code: 1000, msg: '发送成功', data: res};
    }else {
      res = res._hex;
      res = ctx.helper.hexToDec(res);
      ctx.body = { code: 1000, msg: '发送成功', data: res};
    }

  }

  // 查询一个代币地址的小数位
  async getTokenDecimals() {
    const { app, ctx, service } = this;
    // 合约部署的区块高度
    const tokenAddress = ctx.request.body.address;
    try {
      const res = await service.erc20.decimals2(tokenAddress);
      ctx.body = { code: 1000, msg: '发送成功', data: res};
    }catch (e) {
      ctx.body = { code: 1001, msg: '查询失败', data: {}};
    }

  }


}

module.exports = OtherController;
